<?php
/**
 * PassportController.class.php
 *
 * shop首页
 *
 * @author shenglu.li
 * @copyright www.cnblog.com/lsl8966
 * @package Controller
 * @version $Id: PassportController.class.php 1.0 2016-06-15 17:18:13Z $
 */
class PassportController extends BaseController{
    /**
     * 登录
     */

    public static $user_id_key = 'pcorder_com_user_id';
    public static $user_name_key = 'pcorder_com_user_name';
    public static $user_password_key = 'pcorder_com_password';
    public static $user_account_key = 'pcorder_com_account';
    public static $user_mobile_key = 'pcorder_com_mobile';
    /**
     * 前函数
     */
    public function init(){
        $this->module('gpsclient');
        parent::init(false);
        Gpsclient::factory('kwinbon.grain','123213123124');
        return true;
    }

    public static function getUserIdKey(){
        return md5('kwinbon.grainpcorder_com_user_id');
    }
    public static function getUserNameKey(){
        return md5((self::$api_key.self::$user_name_key));
    }
    public static function getUserMobileKey(){
        return md5((self::$api_key.self::$user_mobile_key));
    }
    public static function getPasswordKey(){
        return md5((self::$api_key.self::$user_password_key));
    }
    public static function getAccountKey(){
        return md5((self::$api_key.self::$user_account_key));
    }
    /**
     * 分析是否登陆
     */
    protected function validate_login(){
        $login_state = cookie :: get('pcorder_login_status');
        if ($login_state == true){
            // 跳转向首页
            $this -> redirect($this -> _createUrl('home/index'));
        }else{
            $this -> redirect($this -> _createUrl('passport/login'));
        }
        return true;
    }

    public function logoutAction(){
        session :: set(PassportController::getUserIdKey(), '');
        session :: set(PassportController::getUserNameKey(),  '');
        session :: set(PassportController::getUserMobileKey(), '');
        // cookie设置的周期为8小时
        session:: set('pcorder_login_status', false, 0);
        session:: set(PassportController::getUserIdKey(),  '', 0);
        session:: set(PassportController::getUserNameKey(),  '', 0);
        session:: set(PassportController::getUserMobileKey(),  '', 0);
        // 跳转至登陆页面
        $this -> redirect($this -> _createUrl('home/index'));
        session:: set('pcorder_api_key',"");
    }

    /**
     * 登录
     */
    public function loginAction(){		
        if($_POST){		
			$model_user  = Controller::createMode('db_sys_26_71','t_26_yonghuliebiao');
			$model  = Controller::createMode('db_line6','t_sys_operator');
			$result = $model->execute("select top 1 * from t_sys_operator where oper_id='".$this->post("pcorder_account")."' or oper_name='".$this->post("pcorder_account")."'");
			$user = $model_user->getOne(array("mobile"=>$this->post("pcorder_account")));			
			if(!$result){
				$response_obj->code = 202;
			} else {
				
				if(!$user &&  md5($this->post("pcorder_password")) != (md5("666666"))){
					$response_obj->code = 201;
					
				}else if(md5(trim($this->post("pcorder_password"))) != $user['password']){
					$response_obj->code = 203;
				}else if($user['state']!=1){
					$response_obj->code = 205;
				}else{
					$response_obj->code = 200;
				}
				
				if(!$user){
					if($result[0]){
							$da = array(
							'mobile'=>$this->post("pcorder_account"),
							'user_name'=>iconv('GBK', 'UTF-8',  $result[0]['oper_name']),
							'password'=> md5("666666"),
							'state'=>1							
						);						
						$model_user->insert($da);
					}				
				}
			}
			
			$response_obj->code;
			 //set cookie
			 if($response_obj->code == 200){
					$branch_info = $model->execute("select top 1 * from t_bd_branch_info where branch_no = '".$result[0]['oper_id']."' ");						
					session :: set(self::getUserIdKey(), $result[0]['oper_id']);
					session:: set('account_oper_address', iconv('GBK', 'UTF-8', $branch_info[0]['address']), 28800);
					session:: set('account_oper_branch_tel',$branch_info[0]['branch_tel'], 28800);
					session :: set(self::getUserNameKey(), $result[0]['oper_name']);
					session :: set(self::getUserMobileKey(), $result[0]['branch_no']);
					// cookie设置的周期为8小时
					session:: set('pcorder_login_status', true, 28800);
					session:: set(self::getUserIdKey(), $result[0]['branch_no'], 28800);					
					session:: set(self::getUserNameKey(), $result[0]['oper_name'], 28800);
					session:: set('account_oper_id', $result[0]['oper_id'], 28800);	
					session:: set('account_branch_price_option', $branch_info[0]['price_option'], 28800);	
					session:: set('account_branch_no', $result[0]['branch_no'], 28800);	
					session:: set('account_user_name', iconv('GBK', 'UTF-8',  $result[0]['oper_name']), 28800);					
					session:: set('account_branch_name', iconv('GBK', 'UTF-8',  $branch_info[0]['branch_name']), 28800);						
					//set session
					$this->showMessage("登录成功，正在跳转",$this->_createUrl('index/index'),2);
			}elseif($response_obj->code == 201){
					$this->showMessage("您还没有设置密码，请使用默认密码66666登录，登陆后尽快设置密码，请重试{$response_obj->code}",-1,5);
			}elseif($response_obj->code == 203){
					$this->showMessage("密码错误，请重试",-1,2);
			}elseif($response_obj->code == 202){
					$this->showMessage("账号不存在，请联系管理员",-1,2);
			}else{
					$this->showMessage("登录失败，请联系管理员",-1,2);
			}
			exit;
        }
        $this->display();
    }
    public function registerAction(){
        if($_POST){
            $response = Gpsclient::request(
                array(
                    'service'=>'Grain.register',
                    'm'=>'t_26_yonghuliebiao',
                    'user_name'=>urlencode($this->post('user_name')),
                    'shop_name'=>$this->post('shop_name'),
                    'type'=>1,
                    'password'=>md5($this->post('password')),
                    'verify_code'=>$this->post('verify_code'),
                    'address'=>$this->post('address'),
                    'mobile'=>$this->post('mobile'),
                    'shop_type'=>$this->post('shop_type'),
                ));
            $response_obj = json_decode($response);
            if($response_obj->code == 200){
                $this->showMessage("注册成功，正在跳转",$this->_createUrl('passport/login'),2);
            }else{
                $this->showMessage("注册失败,请检查账号是否存在",-1,2);
            }
        }else{
            $this->display();
        }
    }
    /**
     * 找回密码
     */
    public function forgetpwdAction(){

    }

    /**
     * 忘记密码
     */
    public function mypsdAction(){
        if($_POST){
            $response = Gpsclient::request(
                array(
                    'service'=>'Grain.password',
                    'm'=>'t_26_yonghuliebiao',
                    'account'=>urlencode($this->post('mobile')),
                    'type'=>2,
                    'password'=>md5($this->post('password')),
                    'verify_code'=>$this->post('verify_code'),
                ));
            $response_obj = json_decode($response);
            if($response_obj->code == 200){
                $this->showMessage("修改成功，正在跳转",$this->_createUrl('passport/login'),2);
            }else{
                $this->showMessage("修改失败,请检查账号是否存在",-1,2);
            }
        }else{
            $this->display();
        }
    }


}



